﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Dry.DbInterpreter;
using System.IO;

namespace Dry.Generators
{
    public class EnumGenerator : GeneratorBase
    {
        public static GeneratorFeedback CreateFile(string strTableName)
        {
            GeneratorFeedback generatorFeedback = new GeneratorFeedback();

            string strCode = GetEnumClass(strTableName);
            generatorFeedback.Exception = CreateFileFromString("Enums\\AutoGenerated\\" + strTableName.Substring(3, strTableName.Length - 3) + ".cs", strCode);
            generatorFeedback.Lines = CountLinesInString(strCode);

            return generatorFeedback;
        }
        
        public static string GetEnumClass(string strTableName)
        {
            string strCode = String.Empty;
            SqlDataReader sqlDataReader = DbTableInfo.GetDbTableInfo(strTableName);

            strCode += GetFileHeader(Dry.ProjectName, "", "", "", "", "");

            strCode += "using System;" + _strNewLine;
            strCode += "using System.Collections.Generic;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".DAL;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".IP;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".BO;" + _strNewLine;
            strCode += _strNewLine;
            strCode += "namespace " + Dry.ProjectName + ".Enums" + _strNewLine;
            strCode += "{" + _strNewLine;
            strCode += "    public enum " + strTableName.Substring(3) + _strNewLine;
            strCode += "    {" + _strNewLine;
            strCode += "   " + _strNewLine;
            

            while(sqlDataReader.Read())
            {
                strCode += "        " + sqlDataReader["EnumName"].ToString().Replace(" ", "_") + " = " + sqlDataReader["Id"] + "," + _strNewLine;
            }
            strCode = strCode.TrimEnd(("," + _strNewLine).ToCharArray()) + _strNewLine;

            strCode += "    }" + _strNewLine;
            strCode += "}" + _strNewLine;

            return strCode;
        }

        public static void CreateTableColumnFile()
        {
            CreateFileFromString("Enums\\AutoGenerated\\TableColumnEnum.cs", GetTableColumnEnumClass());
            
        }

        private static string GetTableColumnEnumClass()
        {
            string strCode = String.Empty;
            SqlDataReader sqlDataReader = DbTableColumnInfo.GetDbTableColumnInfo();

            strCode += GetFileHeader(Dry.ProjectName, "", "", "", "", "");

            strCode += "using System;" + _strNewLine;
            strCode += "using System.Collections.Generic;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".DAL;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".IP;" + _strNewLine;
            strCode += "using " + Dry.ProjectName + ".BO;" + _strNewLine;
            strCode += _strNewLine;
            strCode += "namespace " + Dry.ProjectName + ".Enums" + _strNewLine;
            strCode += "{" + _strNewLine;
            strCode += "    public enum TableColumnEnum" + _strNewLine;
            strCode += "    {" + _strNewLine;
            strCode += "   " + _strNewLine;


            while (sqlDataReader.Read())
            {
                strCode += "        " + sqlDataReader["TableColumnEnum"].ToString() + "," + _strNewLine;
            }
            strCode = strCode.TrimEnd(("," + _strNewLine).ToCharArray()) + _strNewLine;

            strCode += "    }" + _strNewLine;
            strCode += "}" + _strNewLine;

            return strCode;
        }
    }
}
